package net.expectx.pay.controller;

import com.alibaba.fastjson.JSONObject;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;

import net.expectx.common.single.util.MonitorCurrentSystemUtil;
import net.expectx.common.single.util.RedisUtil;
import net.expectx.common.single.util.formatter.MessageFormatter;
import net.expectx.pay.common.BaseController;
import net.expectx.pay.dao.bean.MerchantUser;
import net.expectx.pay.dao.bean.MerchantUserExample;
import net.expectx.pay.dao.bean.PaymentChannel;
import net.expectx.pay.dao.bean.PaymentChannelExample;
import net.expectx.pay.rpc.api.MerchantUserService;
import net.expectx.pay.rpc.api.PaymentChannelService;
import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.servlet.ModelAndView;
import redis.clients.jedis.Jedis;

import java.util.List;


/**
 * 后台controller
 * @author jianhun
 * @date 2018/5/17
 */
@Controller
@RequestMapping("/manager/")
@Api(value = "后台管理", description = "后台管理")
public class ManagerController extends BaseController {

    private static final Logger LOGGER = Logger.getLogger(ManagerController.class);
	@Autowired
	private PaymentChannelService paymentChannelService;
	@Autowired
	private MerchantUserService merchantUserService;
	@ApiOperation(value = "后台首页")
	@RequestMapping(value = "index", method = RequestMethod.GET)
	public ModelAndView index(ModelMap modelMap) {
		PaymentChannelExample paymentChannelExample = new PaymentChannelExample();
		PaymentChannelExample.Criteria criteria = paymentChannelExample.createCriteria();
		criteria.andIsDeletedEqualTo(Byte.valueOf("0"));
		List<PaymentChannel> paymentChannels = paymentChannelService.selectByExample(paymentChannelExample);
		MerchantUserExample merchantUserExample = new MerchantUserExample();
		MerchantUserExample.Criteria criteria1 = merchantUserExample.createCriteria();
		criteria1.andIsDeletedEqualTo(Byte.valueOf("0"));
		List<MerchantUser> merchantUsers = merchantUserService.selectByExample(merchantUserExample);
        LOGGER.info(MessageFormatter.format("-----------------------{}-------------------------------","进入首页"));
        ModelAndView modelAndView=new ModelAndView();
        modelAndView.setViewName("index");


        //添加redis
		Jedis jedis = RedisUtil.getJedis();

		for (PaymentChannel paymentChannel : paymentChannels) {
			String value = jedis.hget("EXPAY:PAYMENT_CHANEL:",paymentChannel.getChannelAttr());
			if(value==null){
				jedis.hset("EXPAY:PAYMENT_CHANEL:", paymentChannel.getChannelAttr(), JSONObject.toJSONString(paymentChannel));
			}
//			else{
//				jedis.hdel("EXPAY:PAYMENT_CHANEL:",paymentChannel.getChannelAttr());
//			}

		}
		for (MerchantUser merchantUser : merchantUsers) {
			String value = jedis.hget("EXPAY:MERCHANT_USER:",merchantUser.getMerchantNo());
			if(value==null){
				jedis.hset("EXPAY:MERCHANT_USER:", merchantUser.getMerchantNo(), JSONObject.toJSONString(merchantUser));
			}
		}
		return modelAndView;

	}
	@ApiOperation(value = "后台欢迎页")
	@RequestMapping(value = "console", method = RequestMethod.GET)
	public String welcome(ModelMap modelMap){
		return "console";
	}
    @RequestMapping(value = "/monitorCurrentSystem", method = RequestMethod.GET)
    @ResponseBody
    public Object monitorCurrentSystem(){
        MonitorCurrentSystemUtil service = new MonitorCurrentSystemUtil();
        MonitorCurrentSystemUtil.MonitorCurrentSystemInfo monitorInfo = service.getMonitorCurrentSystemInfo();
	    return monitorInfo;
    }

}
